/*
 * SwingDataEditor.java - Copyright Apr 27, 2012 Z Networks Group, Inc. All rights reserved.
 * Z Networks Group PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.andromeda.skadi.sandbox2;

import com.andromeda.skadi.Application;
import com.andromeda.skadi.data.DataChange;
import com.andromeda.skadi.entity.attribute.Attribute;
import com.andromeda.skadi.service.ConversionService;
import com.andromeda.skadi.property.Property;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import java.io.Serializable;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Dickinson Roman Arismendy Torres
 */
public class SwingDataEditor<T extends Serializable> extends JPanel implements DataEditor<T> {

    protected SwingValueEditor valueEditor;
    protected JLabel lblPropertyName;
    private ChangeHistory<T> changes = new ChangeHistory<T>();
    private transient boolean isChanging = false;
    private Attribute<T> attribute;

    public SwingDataEditor() {
        lblPropertyName = new JLabel("<Property Name>:");
        valueEditor = new SwingTextValueEditor();
        valueEditor.addValueChangeListener(new ValueChangeHandler());
        layoutComponents();
        changes.preview().onChange(this, "applyChangesToValueEditor");
    }

    protected void layoutComponents() {
        setLayout(new FormLayout("pref, 2dlu, pref:grow", "pref:grow"));
        CellConstraints cc = new CellConstraints();
        add(lblPropertyName, cc.rc(1, 1));
        add(valueEditor.getComponent(), cc.rc(1, 3));
    }

    @Override
    public void setData(Attribute<T> attribute) {
        this.attribute = attribute;
        changes.setOriginalValue(attribute.getValue());
        applyChangesToValueEditor();
    }

    @Override
    public DataChange<T> getChange() {
        return changes.getLastChange();
    }

    @Override
    public Property<Boolean> isChanged() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void undo() {
        changes.undo();
    }

    @Override
    public Property<Boolean> canRedo() {
        return changes.canRedo();
    }

    @Override
    public Property<Boolean> canUndo() {
        return changes.canUndo();
    }

    @Override
    public void redo() {
        changes.redo();
    }

    private ConversionService getConversionService() {
        return Application.instance().getService(ConversionService.class);
    }

    private void applyChangesToValueEditor() {

//        Class<T> attributeType = attribute.getType();
//        T currentValue = changes.preview().get();
//        Class<Serializable> displayedType = valueEditor.getRenderedType();
//        Serializable displayedValue = null;
//
//        if (!attributeType.equals(valueEditor.getRenderedType())) {
//            displayedValue = getConversionService().convert(displayedType, currentValue);
//        } else {
//            displayedValue = currentValue;
//        }
//        try {
//            isChanging = true;
//            valueEditor.setValue(displayedValue);
//        } finally {
//            isChanging = false;
//        }
    }

    private void applyChangesFromValueEditor() {
//        Serializable newVal = valueEditor.getValue();
//        T convertedValue = getConversionService().convert(attribute.getAttributeType(), newVal);
//        changes.addChange(new SetValueDataChange<T>(convertedValue));
    }

    private class ValueChangeHandler implements ValueChangeListener<Object> {

        @Override
        public void valueChanged() {
            if (!isChanging) {
                System.out.println("Change");
                applyChangesFromValueEditor();
            }
        }
    }
}
